package views;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

import controllers.Vehicle;
import controllers.VehicleType;

import db.VehicleDAO;
import db.VehicleTypeDAO;

public class AddVehicleFrame extends JFrame {

	private static final int WIDTH = 475;
	private static final int HEIGHT = 250;
	private static final String TITLE = "Add Vehicle";
	private JTextField name;
	private JLabel nameLabel, vehicleTypeLabel;
	private static JComboBox vehicleType;
	private JButton newVehicleType, saveButton;
	private List<VehicleType> vehicleTypes;

	public AddVehicleFrame(int xBound, int yBound){
		this.setTitle(TITLE);
		this.setBounds(xBound+100,yBound+100,WIDTH, HEIGHT);
		this.setResizable(false);

		Container c = this.getContentPane();

		JPanel panel = new JPanel();
		panel.setLayout(null);

		nameLabel = new JLabel("Vehicle Name");
		nameLabel.setBounds(25,25,100,25);
		panel.add(nameLabel);

		name = new JTextField();
		name.setBounds(25,50, 250, 25);
		name.addKeyListener(new KeyListener(){
			@Override
			public void keyPressed(KeyEvent arg0) {
				if(name.getText().length()>0){
					saveButton.setEnabled(true);
				}
				else{
					saveButton.setEnabled(false);
				}
			
			}

			@Override
			public void keyReleased(KeyEvent arg0) {
				if(name.getText().length()>0){
					saveButton.setEnabled(true);
				}
				else{
					saveButton.setEnabled(false);
				}
			
			}

			@Override
			public void keyTyped(KeyEvent arg0) {
				if(name.getText().length()>0){
					saveButton.setEnabled(true);
				}
				else{
					saveButton.setEnabled(false);
				}
			}
		});
		panel.add(name);

		vehicleTypeLabel = new JLabel("Vehicle Type");
		vehicleTypeLabel.setBounds(25, 75, 100, 25);
		panel.add(vehicleTypeLabel);

		vehicleType = new JComboBox();
		vehicleType.setBounds(25,100, 250, 25);
		vehicleTypes = getVehicleTypes();
		setVehicleTypeList(vehicleTypes);
		panel.add(vehicleType);

		newVehicleType = new JButton("New Vehicle Type");
		newVehicleType.setBounds(285, 100, 150, 25);
		newVehicleType.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				new AddVehicleTypeFrame(getXBound(),getYBound());
			}
		});
		panel.add(newVehicleType);

		saveButton = new JButton("Save");
		saveButton.setBounds(25, 150, 100, 25);
		saveButton.setEnabled(false);
		saveButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Vehicle v = new Vehicle();
				v.setName(name.getText());
				v.setVehicleTypeID(vehicleTypes.get(vehicleType.getSelectedIndex()).getId());
				VehicleDAO.addVehicle(v);
				ViewVehiclesFrame.updateRows();
				AddVehicleFrame.this.dispose();
			}
		});
		panel.add(saveButton);

		c.add(panel);
		
		this.setVisible(true);

	}

	public static List<VehicleType> getVehicleTypes(){
		return VehicleTypeDAO.getVehicleTypes();
	}

	public  void setVehicleTypeList(List<VehicleType> list){
		for(int i=0; i<list.size();i++){
			vehicleType.addItem(list.get(i));
		}
	}
	
	public static void updateVehicleTypeList(){
		List<VehicleType> list = getVehicleTypes();
		vehicleType.removeAllItems();
		for(int i=0; i<list.size();i++){
			vehicleType.addItem(list.get(i));
		}
		
	}
	
	public int getXBound(){
		return this.getX();
	}
	
	public int getYBound(){
		return this.getY();
	}

}
